import axios from '@/utils/request'
import { showToast } from 'vant'

function dictionaryList(key) {
  return axios.post(`/dictionary/list/${key}`)
}

function getDictionary(key, cb) {
  dictionaryList(key)
    .then(res => {
      if (res.code === 200) {
        let data = []
        for (const item of res.data) {
          data.push({ text: item.dictLabel, value: item.dictValue })
        }
        cb(data)
      } else {
        showToast({
          type: 'fail',
          message: res.message,
          duration: 1500
        })
      }
    })
    .catch(e => {
      showToast({
        type: 'fail',
        message: e.message,
        duration: 1500
      })
    })
}

export { dictionaryList, getDictionary }
